import tensorflow as tf
"""
    L1、L2 loss的正则化：
    
        loss = loss + ln_regularizer
    
    来防止模型的过拟合
    
"""
if __name__ == '__main__':

    # loss = tf.reduce_mean(tf.square(y-y_) + tf.contrib.layers.l2_regularizer(lambda)(w))
    weights = tf.Variable(tf.random_normal(shape=[1, 2]), tf.float32)

    # l1_regularizer 为正则化项的权重，weights为需要正则化的值
    loss_regular = tf.contrib.layers.l1_regularizer(.8)(weights)
    loss_regular2 = tf.contrib.layers.l2_regularizer(.8)(weights)

    with tf.Session() as session:
        session.run(tf.global_variables_initializer())
        print(session.run(loss_regular))
        print(session.run(loss_regular2))
